﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Majiang.Question;

namespace Majiang.Answer
{
    class Albin: IWin
    {
        int flag = 0;
        public bool IsWin(string str)
        {
            int len = str.Length;
            int[] A = new int[34];
            for (int i = 0; i < len; i++)
            {
                int num = str[i] - '1';
                if (num < 24)
                {
                    A[num]++;
                }
                else
                {
                    A[num - 24]++;
                }
            }
            bool f1 = search(0, 9, A);
            bool f2 = search(16, 25, A);
            bool f3 = search(25, 34, A);
            bool f4 = search1(9, 16, A);
            f1 = f1 && f2 && f3 && f4;
            return f1;
        }
        public bool search(int s, int t, int[] A)
        {
            for (int i = s; i <t; i++)
            {
                if (A[i] == 2)
                {
                    if (flag == 0)
                    {
                        flag = 1;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (A[i] > 3)
                {
                    A[i] = 1;
                }
                if (A[i] == 1)
                {
                    if (i + 2 > A.Length)
                    {
                        return false;
                    }
                    else
                    {
                        A[i + 1]--;
                        A[i + 2]--;
                    }
                }
                if (A[i] == 3)
                {
                    continue;
                }
                if (A[i] < 0)
                {
                    return false;
                }
            }
            return true;
        }
        public bool search1(int s, int t, int[] A)
        {
            for (int i = s; i < t; i++)
            {
                if (A[i] == 2)
                {
                    if (flag == 0)
                    {
                        flag = 1;
                        continue;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (A[i] % 3 != 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}
